import json
import pandas as pd
import datetime
from utils.trans_date import transalte_date
from utils.read_url import get_url
from config import config


def read_type_page_info(type, page_num):
    df = pd.DataFrame()
    url = config.URL.format(page_num, type)
    req_data = get_url(url)
    if req_data:
        json_data = json.loads(req_data).get('ProdList')
        if json_data:
            df = pd.DataFrame(json_data)
            df['collBgnDate'] = df['collBgnDate'].apply(transalte_date)
            df['collEndDate'] = df['collEndDate'].apply(transalte_date)
            df['invalidateDate'] = df['invalidateDate'].apply(transalte_date)
            df['investBgnDate'] = df['investBgnDate'].apply(transalte_date)
            df['investEndDate'] = df['investEndDate'].apply(transalte_date)

            # just today if all relase it
            df = df[df['collBgnDate'] == datetime.datetime.now().strftime('%Y-%m-%d')]

    return df


def read_type_info(type):
    df = pd.DataFrame()
    i = 1
    while True:
        mid_df = read_type_page_info(type, page_num=i)
        if not mid_df.empty:
            df = pd.concat([df, mid_df])
            i += 1
        else:
            break
    return df


def read_main():
    df = pd.DataFrame()
    for type_code in config.TYPE_CODE_LIST:
        mid_df = read_type_info(type = type_code)
        if not mid_df.empty:
            df = pd.concat([df, mid_df])
    return df.sort_values('yieldRate', ascending=False)


df = read_main()
print(df)
print(len(df))
